cc.Class({
    extends: cc.Component,

    properties: {
        btn:cc.Button
        
    },

    // use this for initialization
    onLoad: function () {
        this.btn.node.on(cc.Node.EventType.TOUCH_MOVE, this.onClicked, this);//当手指在屏幕上目标节点区域内移动时
        var avatar = cc.find('NB/YG', this.node); //查找节点
        var originPos = avatar.getPosition();  //获取节点位置
        originPos = avatar.convertToWorldSpace(originPos);//位置对应
        this.originPos = originPos;
    },

    onClicked: function(event){
        var touchPos = cc.p(event.getLocation());

        var pos = cc.find('pos', this.node);
        pos.getComponent('cc.Label').string = '({0}, {1})'.format(touchPos.x, touchPos.y);

        if (touchPos.equals(this.originPos)){
            // do nothing, WHY?
        }
        else {
            // // 计算角度
            // var tempVec = cc.p(touchPos.x - this.originPos.x, touchPos.y - this.originPos.y);

            var directVec = touchPos.sub(this.originPos);
            var angle = cc.p(1,0).signAngle(directVec) * 180 / Math.PI;
            this.updateAvatarRotation(angle);
        }
    },

    // 修改 hero 的方向
    updateAvatarRotation: function(angle){
        cc.find('hero/spr_avatar', this.node).rotation = angle + 90;
    }
});
